//
//  LGConsultRecordMedicineCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/18.
//  咨询记录 - 用药记录

#import "LGConsultRecordMedicineCell.h"

@interface LGConsultRecordMedicineCell()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *typeLabel;
/**   */
@property (nonatomic , strong)UILabel *contentLabel;
/**   */
@property (nonatomic , strong)UILabel *actionLabel;
/**   */
@property (nonatomic , strong)UIView *tipView;

@end

@implementation LGConsultRecordMedicineCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setCanEdit:(BOOL)canEdit{
    _canEdit = canEdit;
    self.tipView.hidden = !canEdit;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    NSString *realityDose = LGNSString(dataDic[@"reality_dose"]);
    self.actionLabel.textColor = [UIColor colorWithString:@"#999999"];
    if([realityDose isEqualToString:@"-1"]){
        //完全按医嘱
        self.actionLabel.text = @"完全按医嘱";
    }else if ([realityDose isEqualToString:@"-2"]){
        //未吃
        self.actionLabel.text = [NSString stringWithFormat:@"停药:%@",dataDic[@"stop_desc"]];
        self.actionLabel.textColor = [UIColor colorWithString:@"#DB453A"];
    }else if (realityDose.length == 0){
        //没选择
        self.actionLabel.text = (self.canEdit == YES)?@"请设置":@"";
    }else{
        //其他用药情况
        self.actionLabel.text = [NSString stringWithFormat:@"%@%@ ,  %@",dataDic[@"reality_dose"],dataDic[@"dose_unit"],dataDic[@"reality_take_frequency"]];
    }
    CGFloat width = [self.actionLabel.text textWidthWithFont:LGFont(14) maxHeight:MAXFLOAT];
    width = (width>Screen_W-viewPix(120))?(Screen_W-viewPix(120)):width;
    [self.actionLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(width+1));
    }];
    if([dataDic[@"advice_type"] isEqualToString:@"in"]){
        self.typeLabel.text = @"医嘱";
        self.actionLabel.hidden = NO;
        self.tipView.hidden = (self.canEdit == YES)?NO:YES;
        self.typeLabel.textColor = [UIColor colorWithString:@"#4C87FD"];
        self.typeLabel.backgroundColor = [UIColor colorWithString:@"#EAF4FF"];
        [self.typeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(viewPix(30)));
        }];
        [self.actionLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(8));
        }];
    }else{
        self.typeLabel.text = @"医嘱外";
        self.typeLabel.textColor = [UIColor colorWithString:@"#31A952"];
        self.typeLabel.backgroundColor = [UIColor colorWithString:@"#ECFBED"];
        self.actionLabel.hidden = YES;
        self.tipView.hidden = YES;
        self.actionLabel.text = @"";
        [self.typeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(viewPix(35)));
        }];
 
        [self.actionLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(0));
        }];
    }
    NSString *content = [NSString stringWithFormat:@"%@ , %@%@ , %@",dataDic[@"medicine_name"],dataDic[@"advice_dose"],dataDic[@"dose_unit"],dataDic[@"advice_take_frequency"]];
    NSMutableAttributedString *attrbutString = [[NSMutableAttributedString alloc]initWithString:content];
    if ([realityDose isEqualToString:@"-2"]){
        //停药 - 划横线
        [attrbutString addAttributes:@{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]} range:NSMakeRange(0, content.length)];
    }
    self.contentLabel.attributedText = attrbutString;
}

-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.contentView addSubview:self.typeLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.actionLabel];
    [self.contentView addSubview:self.tipView];

    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.contentView).offset(viewPix(16));
        make.top.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(12));
        make.left.equalTo(self.contentView).offset(viewPix(32));
        make.width.equalTo(@(viewPix(30)));
        make.height.equalTo(@(viewPix(14)));
    }];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.typeLabel.mas_right).offset(viewPix(6));
        make.right.equalTo(self.contentView).offset(-viewPix(23));
        make.top.equalTo(self.typeLabel).offset(-2);
    }];
    
    [self.actionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(viewPix(8));
        make.left.equalTo(self.contentLabel);
        make.width.equalTo(@(viewPix(50)));
        make.bottom.equalTo(self.contentView).offset(-viewPix(12));
    }];
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.actionLabel.mas_right).offset(viewPix(4));
        make.centerY.equalTo(self.actionLabel);
        make.width.height.equalTo(@(viewPix(12)));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)typeLabel{
    if(!_typeLabel){
        _typeLabel = [UILabel lableWithText:@"医嘱" colorString:@"#FFFFFF" textFont:LGFontWeight(10, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _typeLabel.cornerRidus = viewPix(7);
    }
    return _typeLabel;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

-(UILabel *)actionLabel{
    if(!_actionLabel){
        _actionLabel = [UILabel lableWithText:@"" colorString:@"#3291FF" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _actionLabel;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip12"]];
    }
    return _tipView;
}



@end
